var express = require('express');
var router = express.Router();
var User = require('../models/user');
var Category = require('../models/category');
var Content = require('../models/content');

//定义返回初始化数据
var responseData = {
    code:0,
    message:'初始化',
    _id:'',
    username:''

};
/*router.use(function (req,res,next) {
    responseData = {
        code:0,
        message:'初始化'
    }
    next();
});*/
/**
 * 用户注册接口
 */
router.post('/user/register',function (req, res, next) {
    let username = req.body.username;
    let password = req.body.password;
    let repassword = req.body.repassword;
    //表单验证
    if (username == '') {
        responseData.code = 1;
        responseData.message = '用户名不能为空!'
        res.json(responseData);
        return;
    }
    if (password == '') {
        responseData.code = 2;
        responseData.message = '密码不能为空!'
        res.json(responseData);
        return;
    }
    if (password != repassword) {
        responseData.code = 3;
        responseData.message = '两次密码不一致!'
        res.json(responseData);
        return;
    }
    //验证用户名是否注册
    User.findOne({username: username}).then(function (info) {
        if (info) {
            responseData.code = 0;
            responseData.message = '用户名存在';
            res.json(responseData);
            return;
        }
        else {
            //保存到数据库中
            var user = new User({
                username:username,
                password:password
            });
            user.save(function (err, doc) {
                responseData.code = 4;
                responseData.message = '注册成功';
                res.json(responseData);
                return;
            });
        }
    })
});

/**
 * 登录接口
 */
router.post('/user/login',function (req, res, next) {
    let username = req.body.username;
    let password = req.body.password;
    if (username == '' || password == '') {
        responseData.code = 5;
        responseData.message = "用户名或密码不能为空";
        res.json(responseData);
        return;
    }
    User.findOne({username: username, password:password}).then(function (info) {
        if (!info) {
            responseData.code = 6;
            responseData.message = "用户名或者密码错误";
            res.json(responseData);
            return;
        }else {
            responseData.code = 7;
            responseData.message = "登录成功";
            responseData.username = info.username;
            responseData._id = info._id;
            req.cookies.set('userInfo',JSON.stringify({
                _id:info._id,
            }),{ expires: new Date(Date.now() + 9000000), httpOnly: true });
            res.json(responseData);
            return;
        }
    })
})

/**
 * 退出登录接口
 */
router.post('/user/logout',function (req, res, next) {
    req.cookies.set('userInfo',null);//清除cookies
    responseData.code = 8;
    responseData.message = '用户名已退出';
    res.json(responseData);
})

/**
 * 读取用户列表接口,返回pages
 */
router.get('/admin/getAllUserInfo',function (req, res) {
    // console.log(req.body);
    var limit = Number(req.query.limit) || 10;
    var skip = Number(req.query.offset) || 0;
    User.count().then(function (count) {
        User.find().limit(limit).skip(skip).then(function (users) {
            //"number":1,"size":10,"totalRecords":3,"records":
            var page = {
                page:skip+1,
                size:limit,
                total:count,
                rows:users
            }
            res.json(page)
        })
    })


})


/**
 * 读取分类接口,返回pages
 */
router.get('/admin/getAllCategoryInfo',function (req, res) {
    // console.log(req.body);
    var limit = Number(req.query.limit) || 10;
    var skip = Number(req.query.offset) || 0;
    Category.count().then(function (count) {
        Category.find().limit(limit).skip(skip).then(function (users) {
            var page = {
                page:skip+1,
                size:limit,
                total:count,
                rows:users
            }
            res.json(page)
        })
    })


})

/**
 * 读取分类接口,返回pages
 */
router.get('/admin/getAllContentInfo',function (req, res) {
    // console.log(req.body);
    var limit = Number(req.query.limit) || 10;
    var skip = Number(req.query.offset) || 0;
    Content.count().then(function (count) {
        Content.find().limit(limit).skip(skip).populate('category').then(function (users) {
            var page = {
                page:skip+1,
                size:limit,
                total:count,
                rows:users
            }
            res.json(page)
        })
    })
})
module.exports = router;